<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="search">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话号码" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="VIP等级" prop="vipLevel">
        <el-select v-model="queryParams.vipLevel" placeholder="积分类型" clearable style="width: 240px">
          <el-option v-for="dict in vipLevelList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="获取方式" prop="opType">
        <el-select v-model="queryParams.opType" placeholder="获取方式" clearable style="width: 240px">
          <el-option v-for="dict in opTypeList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="VIP结束时间">
        <el-date-picker v-model="dateRange" style="width: 340px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :height="tableHeight" @sort-change="changeTableSort" v-loading="loading" :data="list" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="创建时间" prop="createTime" width="160" align="center" :sortable="'custom'">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="UID" prop="userId" align="center" :sortable="'custom'" />
      <el-table-column label="昵称" prop="userNickName" align="center" />
      <el-table-column label="电话号码" prop="phone" align="center" width="120" />
      <el-table-column label="VIP开始时间" prop="vipStartTime" align="center" width="160" :sortable="'custom'" />
      <el-table-column label="VIP截止时间" prop="vipEndTime" align="center" width="160" :sortable="'custom'" />
      <el-table-column label="VIP等级" prop="vipLevel" align="center" width="120" :sortable="'custom'">
        <template slot-scope="scope">
          <span>{{selectDictLabel(vipLevel,scope.row.vipLevel)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="获取方式" prop="opType" align="center" width="120" :sortable="'custom'">
        <template slot-scope="scope">
          <span>{{selectDictLabel(opType,scope.row.opType)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" align="center" min-width="160" />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <z-dialog :title="title" :visible.sync="open" :isShowConfirm="false">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="form.createTime" :disabled="true" />
        </el-form-item>
        <el-form-item label="UID" prop="userId">
          <el-input v-model.number="form.userId" :disabled="true" />
        </el-form-item>
        <el-form-item label="昵称" prop="userNickName">
          <el-input v-model="form.userNickName" :disabled="true" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="form.phone" :disabled="true" />
        </el-form-item>
        <el-form-item label="VIP开始时间" prop="vipStartTime">
          <el-input v-model="form.vipStartTime" :disabled="true" />
        </el-form-item>
        <el-form-item label="VIP截止时间" prop="vipEndTime">
          <el-input v-model="form.vipEndTime" :disabled="true" />
        </el-form-item>
        <el-form-item label="VIP等级" prop="vipLevel">
          <el-select v-model="form.vipLevel" clearable style="width: 100%" disabled>
            <el-option v-for="dict in vipLevel" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="获取方式" prop="opType">
          <el-select v-model="form.opType" clearable style="width: 100%" disabled>
            <el-option v-for="dict in opTypeList" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :disabled="true" />
        </el-form-item>
      </el-form>
    </z-dialog>
  </div>
</template>

<script>
import { appLogVipList } from "@/api/vip/vip.js";
import { getLabelByValue } from "@/common/common"

export default {
  name: "VipRecord", //vip购买记录
  dicts: ["basics_information_status"],
  data() {
    return {
      // 列表数据
      list: [],

      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      pageNum: 1,
      // 表单参数
      form: {},

      dateRange: [],

      opType: [
        { label: '购买席位', value: '0' },
        { label: '自动升级', value: '1' },
        { label: '管理端操作', value: '2' },
        { label: '新用户注册', value: '3' },
        { label: '自动降级', value: '4' },
        { label: '分享升级', value: '6' },
        { label: '抽奖', value: '8' },
        { label: '购买消费品赠送', value: '11' },
        { label: '开馆之礼出价', value: '12' }
      ],

      //VIP等级（0普通用户，1有效会员，2嘉宾，3黄金）
      vipLevel: [
        { label: '普通用户', value: 0 },
        { label: '有效会员', value: 1 },
        { label: '银牌会员', value: 2 },
        { label: '金牌会员', value: 3 }
      ]
    };
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    dateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.vipEndTimeStart = String(newV[0]);
          this.queryParams.vipEndTimeEnd = String(newV[1]);
        }
      } else {
        this.queryParams.vipEndTimeStart = undefined;
        this.queryParams.vipEndTimeEnd = undefined;
      }
      this.getList();

    },
  },
  created() {
    this.getList();
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
    //
    opTypeList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.opType));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    vipLevelList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.vipLevel));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },

  },
  methods: {
    getList() {
      this.loading = true;
      appLogVipList(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.list = response.data;
          this.total = response.total;
        }
        this.loading = false;
      });
    },

    changeTableSort(column) {
      this.queryParams.orderBy = column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正
      this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        preId: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.vipEndTimeStart = undefined;
      this.queryParams.vipEndTimeEnd = undefined;
      this.dateRange = undefined;
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "VIP购买记录";
      this.open = true;
      this.form = row;
    },

    /** 提交按钮 */
    submitForm(from) {

    },

    getLabel(arr, value) {
      return getLabelByValue(arr, value);
    }
  },
};
</script>
<style scoped>
</style>
